﻿using MESBaseis.Domain.BaseEntities;
using MESBaseis.Domain.XZY;
using MESBaseis.Domain.ZSHH;
using MESBaseis.Domain.ZSH;
using Microsoft.EntityFrameworkCore;
using MESBaseis.Domain.BOM;

namespace MESData.Infrastucture
{
    public class MyDbcontext : DbContext
    {
        public MyDbcontext(DbContextOptions options) : base(options)
        {
        }

        // ZSHH命名空间下的DbSet属性
        public DbSet<Item> Items { get; set; }
        public DbSet<ItemInventory> ItemInventories { get; set; }
        public DbSet<Project> Projects { get; set; }
        public DbSet<SalesOrder> SalesOrders { get; set; }
        public DbSet<SalesOrderItem> SalesOrderItems { get; set; }
        public DbSet<ProductionPlan> ProductionPlans { get; set; }
        public DbSet<ProductionPlanItem> ProductionPlanItems { get; set; }
        public DbSet<ProductionPlanAttachment> ProductionPlanAttachments { get; set; }
        public DbSet<ProductionPlanOperationLog> ProductionPlanOperationLogs { get; set; }
        public DbSet<ProductedItem> ProductedItems { get; set; }
        public DbSet<ProductionPlanSalesOrder> ProductionPlanSalesOrders { get; set; }

        // XZY命名空间下的DbSet属性
        public DbSet<ProductionItemModel> ProductionItems { get; set; }
        public DbSet<ProductionOrderModel> ProductionOrders { get; set; }
        public DbSet<ProductionSalesOrderModel> ProductionSalesOrders { get; set; }
        public DbSet<PurchaseOrderModel> PurchaseOrders { get; set; }
        public DbSet<ProcessModel> Processes { get; set; }
        public DbSet<WarehouseModel> Warehouses { get; set; }
        public DbSet<AnnouncementModel> Announcements { get; set; }
        public DbSet<ApprovalDocumentModel> ApprovalDocuments { get; set; }
        public DbSet<CoreMetricsModel> CoreMetrics { get; set; }
        public DbSet<StatisticsModel> Statistics { get; set; }
        public DbSet<UserNotificationModel> UserNotifications { get; set; }

        // ZSH命名空间下的DbSet属性
        public DbSet<ProcessCategory> ZshProcessCategories { get; set; }
        public DbSet<Process> ZshProcesses { get; set; }
        public DbSet<ProcessFlow> ZshProcessFlows { get; set; }
        public DbSet<ProcessFlowStep> ZshProcessFlowSteps { get; set; }

        public DbSet<ManagementModel> Management { get; set; }
        public DbSet<ProductModel> Product { get; set; }

        // BaseEntities
        public DbSet<User> Users { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            // ZSHH命名空间下的表名配置
            modelBuilder.Entity<Item>().ToTable("Items");
            modelBuilder.Entity<ItemInventory>().ToTable("ItemInventories");
            modelBuilder.Entity<Project>().ToTable("Projects");
            modelBuilder.Entity<SalesOrder>().ToTable("SalesOrders");
            modelBuilder.Entity<SalesOrderItem>().ToTable("SalesOrderItems");
            modelBuilder.Entity<ProductionPlan>().ToTable("ProductionPlans");
            modelBuilder.Entity<ProductionPlanItem>().ToTable("ProductionPlanItems");
            modelBuilder.Entity<ProductionPlanAttachment>().ToTable("ProductionPlanAttachments");
            modelBuilder.Entity<ProductionPlanOperationLog>().ToTable("ProductionPlanOperationLogs");

            // XZY命名空间下的表名配置
            modelBuilder.Entity<ProductionItemModel>().ToTable("ProductionItemModels");
            modelBuilder.Entity<ProductionOrderModel>().ToTable("ProductionOrderModels");
            modelBuilder.Entity<ProductionSalesOrderModel>().ToTable("ProductionSalesOrderModels");
            modelBuilder.Entity<PurchaseOrderModel>().ToTable("PurchaseOrderModels");
            modelBuilder.Entity<ProcessModel>().ToTable("ProcesseModels");
            modelBuilder.Entity<WarehouseModel>().ToTable("Warehouses");
            modelBuilder.Entity<AnnouncementModel>().ToTable("AnnouncementModels");
            modelBuilder.Entity<ApprovalDocumentModel>().ToTable("ApprovalDocumentModels");
            modelBuilder.Entity<CoreMetricsModel>().ToTable("CoreMetricModels");
            modelBuilder.Entity<StatisticsModel>().ToTable("StatisticModels");
            modelBuilder.Entity<UserNotificationModel>().ToTable("UserNotificationModels");

            // ZSH命名空间下的表名配置
            modelBuilder.Entity<ProcessCategory>().ToTable("ZSH_ProcessCategories");
            modelBuilder.Entity<Process>().ToTable("ZSH_Processes");
            modelBuilder.Entity<ProcessFlow>().ToTable("ZSH_ProcessFlows");
            modelBuilder.Entity<ProcessFlowStep>().ToTable("ZSH_ProcessFlowSteps");



    
            modelBuilder.Entity<ManagementModel>().ToTable("Management");
            modelBuilder.Entity<ProductModel>().ToTable("Product");
      

            // ZSH命名空间下的外键关系配置
            // 工序分类 -> 工序 (一对多)
            modelBuilder.Entity<Process>()
                .HasOne(p => p.ProcessCategory)
                .WithMany(pc => pc.Processes)
                .HasForeignKey(p => p.ProcessCategoryId)
                .OnDelete(DeleteBehavior.Restrict);
            
            // 工艺流程 -> 工艺流程步骤 (一对多)
            modelBuilder.Entity<ProcessFlowStep>()
                .HasOne(pfs => pfs.ProcessFlow)
                .WithMany(pf => pf.ProcessFlowSteps)
                .HasForeignKey(pfs => pfs.ProcessFlowId)
                .OnDelete(DeleteBehavior.Restrict);
            
            // 工序 -> 工艺流程步骤 (一对多)
            modelBuilder.Entity<ProcessFlowStep>()
                .HasOne(pfs => pfs.Process)
                .WithMany(p => p.ProcessFlowSteps)
                .HasForeignKey(pfs => pfs.ProcessId)
                .OnDelete(DeleteBehavior.Restrict);

            // BaseEntities表名配置
            modelBuilder.Entity<User>().ToTable("Users");

            // 优化：配置数据库索引以提高查询性能
            ConfigureDatabaseIndexes(modelBuilder);
        }

        /// <summary>
        /// 配置数据库索引以提高查询性能
        /// </summary>
        private void ConfigureDatabaseIndexes(ModelBuilder modelBuilder)
        {
            // 通知公告表索引
            modelBuilder.Entity<MESBaseis.Domain.XZY.AnnouncementModel>()
                .HasIndex(a => new { a.IsDeleted, a.IsActive, a.CreateTime })
                .HasDatabaseName("IX_Announcements_Deleted_Active_CreateTime");

            modelBuilder.Entity<MESBaseis.Domain.XZY.AnnouncementModel>()
                .HasIndex(a => new { a.Type, a.IsDeleted })
                .HasDatabaseName("IX_Announcements_Type_Deleted");

            modelBuilder.Entity<MESBaseis.Domain.XZY.AnnouncementModel>()
                .HasIndex(a => new { a.Publisher, a.IsDeleted })
                .HasDatabaseName("IX_Announcements_Publisher_Deleted");

            // 采购订单表索引
            modelBuilder.Entity<MESBaseis.Domain.XZY.PurchaseOrderModel>()
                .HasIndex(p => new { p.IsDeleted, p.Status, p.CreateTime })
                .HasDatabaseName("IX_PurchaseOrders_Deleted_Status_CreateTime");

            modelBuilder.Entity<MESBaseis.Domain.XZY.PurchaseOrderModel>()
                .HasIndex(p => new { p.OrderNumber, p.IsDeleted })
                .HasDatabaseName("IX_PurchaseOrders_OrderNumber_Deleted");

            modelBuilder.Entity<MESBaseis.Domain.XZY.PurchaseOrderModel>()
                .HasIndex(p => new { p.SupplierName, p.IsDeleted })
                .HasDatabaseName("IX_PurchaseOrders_SupplierName_Deleted");

            modelBuilder.Entity<MESBaseis.Domain.XZY.PurchaseOrderModel>()
                .HasIndex(p => new { p.OrderType, p.IsDeleted })
                .HasDatabaseName("IX_PurchaseOrders_OrderType_Deleted");

            // 生产工单表索引
            modelBuilder.Entity<MESBaseis.Domain.XZY.ProductionOrderModel>()
                .HasIndex(p => new { p.IsDeleted, p.Status, p.CreateTime })
                .HasDatabaseName("IX_ProductionOrders_Deleted_Status_CreateTime");

            modelBuilder.Entity<MESBaseis.Domain.XZY.ProductionOrderModel>()
                .HasIndex(p => new { p.OrderNumber, p.IsDeleted })
                .HasDatabaseName("IX_ProductionOrders_OrderNumber_Deleted");

            modelBuilder.Entity<MESBaseis.Domain.XZY.ProductionOrderModel>()
                .HasIndex(p => new { p.ResponsiblePerson, p.IsDeleted })
                .HasDatabaseName("IX_ProductionOrders_ResponsiblePerson_Deleted");

            // 销售订单表索引
            modelBuilder.Entity<MESBaseis.Domain.XZY.ProductionSalesOrderModel>()
                .HasIndex(p => new { p.IsDeleted, p.Status, p.CreateTime })
                .HasDatabaseName("IX_ProductionSalesOrders_Deleted_Status_CreateTime");

            modelBuilder.Entity<MESBaseis.Domain.XZY.ProductionSalesOrderModel>()
                .HasIndex(p => new { p.OrderNumber, p.IsDeleted })
                .HasDatabaseName("IX_ProductionSalesOrders_OrderNumber_Deleted");

            modelBuilder.Entity<MESBaseis.Domain.XZY.ProductionSalesOrderModel>()
                .HasIndex(p => new { p.CustomerName, p.IsDeleted })
                .HasDatabaseName("IX_ProductionSalesOrders_CustomerName_Deleted");

            // 工序表索引
            modelBuilder.Entity<MESBaseis.Domain.XZY.ProcessModel>()
                .HasIndex(p => new { p.IsDeleted, p.Status, p.CreateTime })
                .HasDatabaseName("IX_Processes_Deleted_Status_CreateTime");

            modelBuilder.Entity<MESBaseis.Domain.XZY.ProcessModel>()
                .HasIndex(p => new { p.DocumentNumber, p.IsDeleted })
                .HasDatabaseName("IX_Processes_DocumentNumber_Deleted");

            modelBuilder.Entity<MESBaseis.Domain.XZY.ProcessModel>()
                .HasIndex(p => new { p.ProcessType, p.IsDeleted })
                .HasDatabaseName("IX_Processes_ProcessType_Deleted");

            // 生产物品表索引
            modelBuilder.Entity<MESBaseis.Domain.XZY.ProductionItemModel>()
                .HasIndex(p => new { p.IsDeleted, p.ItemType, p.CreateTime })
                .HasDatabaseName("IX_ProductionItems_Deleted_Type_CreateTime");

            modelBuilder.Entity<MESBaseis.Domain.XZY.ProductionItemModel>()
                .HasIndex(p => new { p.IsDeleted, p.ItemNumber })
                .HasDatabaseName("IX_ProductionItems_ItemNumber_Deleted");

            modelBuilder.Entity<MESBaseis.Domain.XZY.ProductionItemModel>()
                .HasIndex(p => new { p.IsDeleted, p.ItemName })
                .HasDatabaseName("IX_ProductionItems_ItemName_Deleted");

            // 仓库表索引
            modelBuilder.Entity<MESBaseis.Domain.XZY.WarehouseModel>()
                .HasIndex(w => new { w.IsDeleted, w.WarehouseType, w.CreateTime })
                .HasDatabaseName("IX_Warehouses_Deleted_Type_CreateTime");

            modelBuilder.Entity<MESBaseis.Domain.XZY.WarehouseModel>()
                .HasIndex(w => new { w.WarehouseName, w.IsDeleted })
                .HasDatabaseName("IX_Warehouses_Name_Deleted");

            // 审批文档表索引
            modelBuilder.Entity<MESBaseis.Domain.XZY.ApprovalDocumentModel>()
                .HasIndex(a => new { a.IsDeleted, a.Status, a.CreateTime })
                .HasDatabaseName("IX_ApprovalDocuments_Deleted_Status_CreateTime");

            modelBuilder.Entity<MESBaseis.Domain.XZY.ApprovalDocumentModel>()
                .HasIndex(a => new { a.DocumentNumber, a.IsDeleted })
                .HasDatabaseName("IX_ApprovalDocuments_DocumentNumber_Deleted");

            modelBuilder.Entity<MESBaseis.Domain.XZY.ApprovalDocumentModel>()
                .HasIndex(a => new { a.Approver, a.IsDeleted })
                .HasDatabaseName("IX_ApprovalDocuments_Approver_Deleted");

            // 复合索引 - 提高多条件查询性能
            modelBuilder.Entity<MESBaseis.Domain.XZY.AnnouncementModel>()
                .HasIndex(a => new { a.IsDeleted, a.Type, a.IsActive, a.CreateTime })
                .HasDatabaseName("IX_Announcements_Deleted_Type_Active_CreateTime");

            modelBuilder.Entity<MESBaseis.Domain.XZY.PurchaseOrderModel>()
                .HasIndex(p => new { p.IsDeleted, p.Status, p.OrderType, p.CreateTime })
                .HasDatabaseName("IX_PurchaseOrders_Deleted_Status_Type_CreateTime");

            modelBuilder.Entity<MESBaseis.Domain.XZY.ProductionOrderModel>()
                .HasIndex(p => new { p.IsDeleted, p.Status, p.ResponsiblePerson, p.CreateTime })
                .HasDatabaseName("IX_ProductionOrders_Deleted_Status_Person_CreateTime");

            modelBuilder.Entity<MESBaseis.Domain.XZY.ProductionSalesOrderModel>()
                .HasIndex(p => new { p.IsDeleted, p.Status, p.CustomerName, p.CreateTime })
                .HasDatabaseName("IX_ProductionSalesOrders_Deleted_Status_Customer_CreateTime");
        }
    }
}